package com.wb.spring.boot.autoconfigure.proxy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class WbClientCall {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 实际请求
     * @param uri
     *  请求路径
     * @param method
     *  请求方法
     * @param requestBody
     *  body参数
     * @return
     */
    public <T> T call(String uri, String method, Object requestBody, Class<T> clazz){
        ResponseEntity<T> entityBody = null;
        switch (method) {
            case "GET":
                entityBody = restTemplate.getForEntity(uri, clazz);
                break;
            case "POST":
                entityBody = restTemplate.postForEntity(uri, requestBody, clazz);
                break;
        }

        if (clazz == Void.class) {
            return null;
        }
        return entityBody.getBody();
    }
}
